package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.ApplicationContext;
import com.itheima.mapper.AddressBookDao;
import com.itheima.pojo.AddressBook;
import com.itheima.result.R;
import com.itheima.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: 斌
 * @DateTime: 2023年02月12日  16:21:49
 * @Description: com.itheima.service.Impl
 * @Version: 1.0
 */
@Slf4j
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookDao, AddressBook> implements AddressBookService {
    @Autowired
    private AddressBookDao addressBookDao;
    @Override
    public R addAddress(AddressBook addressBook, HttpSession session) {
        Long userId = (Long) session.getAttribute("user");
        //这里要设置用户ID，用来区别该地址归属于哪个用户
        addressBook.setUserId(userId);
        //将用户添加的地址设置非默认地址0，当用户发送默认地址请求时，再设置为1
        addressBook.setIsDefault(0);
        addressBookDao.insert(addressBook);
        return R.success("保存地址成功");
    }

    @Override
    public R updateDefaultAddress(AddressBook addressBook) {
        //设置默认地址思路，进行修改默认地址是，现将属于该用户的所有地址设置非默认，再将传过来的地址id，把它设置为1
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ApplicationContext.getData() != null,AddressBook::getUserId,ApplicationContext.getData()) //where user_id=?
                .set(AddressBook::getIsDefault,0);

                    //set  isdefault=0
        addressBookDao.update(addressBook,updateWrapper);
        //设置默认地址---1
        //设置用户要设置的默认地址，设置为1
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ApplicationContext.getData() != null,AddressBook::getUserId,ApplicationContext.getData()) //where user_id=?
                .set(AddressBook::getIsDefault,1)
                .eq(addressBook.getId() != null,AddressBook::getId,addressBook.getId());

        //set  isdefault=1
        addressBookDao.update(addressBook,wrapper);

        return R.success("设置成功");
    }

    /**
     *根据当前登录用户ID 以及 is_default进行查询，查询当前登录用户is_default为1的地址信息

     * @return
     */
    @Override
    public R queryDefaultAddress() {
        //select * from addressBook where user_id = ? and is_default =1

        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApplicationContext.getData() !=null,AddressBook::getUserId,ApplicationContext.getData())
                .eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookDao.selectOne(queryWrapper);
        return R.success(one);
    }

    /**
     * 根据当前登录用户ID，查询所有的地址列表 select * from address_book where user_id =  ?
     *
     * @return
     */
    @Override
    public R queryAddressList() {
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApplicationContext.getData() != null,AddressBook::getUserId, ApplicationContext.getData());
        List<AddressBook> addressBooks = addressBookDao.selectList(wrapper);
        return R.success(addressBooks);
    }

    @Override
    public R removeAddressList(List<Long> ids, HttpSession session) {
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId != null,AddressBook::getUserId,userId)
                .in(ids !=null,AddressBook::getId,ids);
        int deleteResult = addressBookDao.delete(wrapper);
        return deleteResult> 0 ? R.success("删除成功") : R.error("删除失败");
    }

    /**
     * 修改地址
     * @param addressBook
     *
     * @return
     */

    @Override
    public R updateAddress(AddressBook addressBook) {


        Long addressBookUserId = addressBook.getUserId();
        int updateResult = 0;
        if (ApplicationContext.getData().equals(addressBookUserId)){
            LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(addressBook.getId() != null,AddressBook::getId,addressBook.getId());
             updateResult = addressBookDao.update(addressBook, updateWrapper);
        }
        return updateResult > 0 ? R.success("修改成功") : R.error("修改失败");
    }

    @Override
    public R queryAddressById(Long id) {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null,AddressBook::getId,id)
                .eq(ApplicationContext.getData() != null,AddressBook::getUserId,ApplicationContext.getData());
        AddressBook addressBook = addressBookDao.selectOne(queryWrapper);
        return R.success(addressBook);
    }
}
